package com.grace.law.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grace.law.bean.Case;
import com.grace.law.bean.CaseExample;
import com.grace.law.bean.CaseExample.Criteria;
import com.grace.law.bean.Msg;
import com.grace.law.service.CaseService;
import com.grace.law.service.DictService;

@Controller
@RequestMapping("/case")
public class CaseController {
	@Autowired
	CaseService caseService;
	@Autowired
	DictService dictService;
	
	
	@RequestMapping("/list")
	@ResponseBody
	public Msg getCasesWithJson(
			@RequestParam(value = "pn", defaultValue = "1") Integer pn,
			@RequestParam Integer customerId,
			@RequestParam String customerName,
			@RequestParam String phone) {
		CaseExample caseExample =new CaseExample();
		Criteria criteria = caseExample.createCriteria();
		if(customerId!=null){
			criteria.andCustomerIdEqualTo(customerId);
		}
		if(!StringUtils.isEmpty(customerName)){
			criteria.andCustomerNameEqualTo(customerName);
		}
		if(!StringUtils.isEmpty(phone)){
			criteria.andPhoneEqualTo(phone);
		}
		List<Case> cases = caseService.getAll(caseExample);
		PageHelper.startPage(pn, 5);
		PageInfo page = new PageInfo(cases, 5);
		return Msg.success().add("pageInfo", page).add("dicts", dictService.getAll());
	}
	
	@RequestMapping(value = "/input", method = RequestMethod.POST)
	@ResponseBody
	public Msg submit(Case record) {
		caseService.insert(record);

		return Msg.success();

	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Msg save(Case record) {
		if(caseService.save(record)<0){
			return Msg.success();
		}

		return Msg.error();
	}
	
	@RequestMapping(value = "/edit/{customerId}",method=RequestMethod.GET)
	public ModelAndView edit(@PathVariable("customerId") String customerId) {
		Case data = caseService.selectCaseById(customerId);
		ModelAndView mv = new ModelAndView("caseedit");
		mv.addObject("customer", data);
		return mv;

	}
}
